﻿package com.ruochi.swfoto{
	import com.ruochi.component.CircleProgressBar;		
	import com.ruochi.shape.RoundRect;
	import com.ruochi.utils.defaultNum;
	import com.ruochi.utils.defaultBoolean;
	import com.ruochi.utils.defaultString;
	import com.ruochi.utils.fullDimension;
	import com.ruochi.utils.replaceHat;
	import com.ruochi.utils.resizeBitmap;
	import flash.display.Loader;
	import flash.display.Stage;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.display.Sprite;
	import flash.events.ErrorEvent;
	import flash.events.MouseEvent;
	import flash.events.Event;
	import flash.net.navigateToURL;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import flash.utils.ByteArray;
	import flash.display.Bitmap;
	public class Swfoto2 extends Sprite {
		private var _imageContainer:Sprite = new Sprite();
		private var _imageContainerMask:RoundRect;
		private var _isShowProgressBar:Boolean = false;
		private var _circleProgressBar:CircleProgressBar;	
		private var _binaryLdr:URLLoader = new URLLoader();
		private var _circleProgressBarColor:uint=0xffffff;
		private var _windowOpen:String = "_blank";
		private var _heightQuality:Boolean = true;
		private var _link:String = "";
		private var _imageWidth:Number;
		private var _imageHeight:Number;
		private var _imageLdr:Loader = new Loader();
		private var _imageUrl:String = "http://swfoto.ruochigroup.com/images/girl.jpg";
		private var _roundCorner:Number = 0;
		private var _isFadeIn:Boolean = true;
		private var _fadeInStep:Number = 0.1;
		private var _isInit:Boolean = false;
		public function Swfoto2() {
			if(stage==null){
				addEventListener(Event.ADDED_TO_STAGE , onAddToStage, false, 0 , true);
			}else {
				addEventListener(Event.ENTER_FRAME, onEnterFrame, false, 0, true);
			}
		}
		private function onAddToStage(e:Event):void {
			removeEventListener(Event.ADDED_TO_STAGE , onAddToStage);
			addEventListener(Event.ENTER_FRAME, onEnterFrame, false, 0, true);
		}
		private function onEnterFrame(e:Event):void {
			if (stage.stageWidth > 0) {
				removeEventListener(Event.ENTER_FRAME, onEnterFrame);
				init();
			}
		}
		private function onStageResize(e:Event):void {
			if(stage.stageWidth != 0){
				stage.removeEventListener(Event.RESIZE, onStageResize);			
				init();
			}
		}
		private function init():void {
			_isInit = true;
			if(parent is Stage){
				stage.align=StageAlign.TOP_LEFT;
				stage.scaleMode = StageScaleMode.NO_SCALE;
			}
			initVar();
			buildUI();
			configListener();
			_binaryLdr.dataFormat = URLLoaderDataFormat.BINARY;
			_binaryLdr.load(new URLRequest(_imageUrl));
		}
		private function buildUI():void {			
			if (_isShowProgressBar) {
				_circleProgressBar = new CircleProgressBar(_imageWidth, _imageHeight);
				_circleProgressBar.loaderColor = _circleProgressBarColor;
				_circleProgressBar.bg.visible = false;
				_circleProgressBar.start();
				addChild(_circleProgressBar);
			}			
			_imageContainerMask = new RoundRect(_imageWidth, _imageHeight, _roundCorner);
			_imageContainerMask.alpha = 0;
			addChild(_imageContainerMask);
			addChild(_imageContainer);
		}
		private function configListener():void {
			_binaryLdr.addEventListener(Event.COMPLETE, onLoadComplete, false, 0, true);
			_imageLdr.contentLoaderInfo.addEventListener(Event.INIT, imageInit, false, 0, true);
			if (_link!="") {
				buttonMode = true;
				_imageContainer.addEventListener(MouseEvent.CLICK, onClick, false, 0, true);
			}	
		}
		private function initVar():void {
			_imageWidth = defaultNum(loaderInfo.parameters["width"], stage.stageWidth);
			_imageHeight = defaultNum(loaderInfo.parameters["height"],stage.stageHeight);
			_roundCorner =  defaultNum(loaderInfo.parameters["roundCorner"], _roundCorner);
			_heightQuality = defaultBoolean(loaderInfo.parameters["heightQuality"], _heightQuality);
			_isShowProgressBar = defaultBoolean(loaderInfo.parameters["isShowLoader"], _isShowProgressBar);
			_isFadeIn = defaultBoolean(loaderInfo.parameters["isFadeIn"], _isFadeIn);
			_windowOpen = defaultString(loaderInfo.parameters["windowOpen"], _windowOpen);
			_circleProgressBarColor = defaultNum(loaderInfo.parameters["loaderColor"], _circleProgressBarColor);
			_link = replaceHat(defaultString(loaderInfo.parameters["link"], _link));
			_imageUrl = replaceHat(defaultString(loaderInfo.parameters["image"], _imageUrl));
			stage.frameRate = defaultNum(loaderInfo.parameters["frameRate"], "24");
		}
		private function onLoadComplete(e:Event):void {
			_imageLdr.loadBytes(e.target.data as ByteArray);
		}
		private function onEndLoadin(e:Event):void {
			_circleProgressBar.stop();
		}
		private function imageInit(e:Event):void {
			var bitmap:Bitmap = e.target.loader.content as Bitmap;
			bitmap.smoothing = true;
			fullDimension(bitmap, _imageWidth, _imageHeight);
			if (bitmap.scaleX<.5&&_heightQuality==true) {
				bitmap.bitmapData = resizeBitmap(bitmap.bitmapData, bitmap.width, bitmap.height);
				fullDimension(bitmap,_imageWidth,_imageHeight);
			}
			_imageContainer.alpha = 0;
			_imageContainer.addChild(bitmap);
			if (_isFadeIn) {
				_imageContainer.addEventListener(Event.ENTER_FRAME, onFrame, false, 0, true);
			}else {
				_imageContainer.alpha = 1;
			}			
			_imageContainer.mask = _imageContainerMask;
		}
		private function onFrame(e:Event):void {
			_imageContainer.alpha += _fadeInStep;
			if (_imageContainer.alpha >= 1) {				
				_imageContainer.removeEventListener(Event.ENTER_FRAME, onFrame);
				_imageContainer.alpha = 1;
				if(_isShowProgressBar){
					_circleProgressBar.stop();
				}
			}
		}
		private function onClick(e:MouseEvent):void {
			navigateToURL(new URLRequest(_link), _windowOpen);
		}
	}
}